﻿package  {
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import com.greensock.TweenLite;
	import com.greensock.easing.Linear;
	import flash.media.Sound;
	
	public class Results extends MovieClip{
		private var Score:Number = 0;
		private var Score_Array:Array;
		private var SoupName:String;
		
		private var recipeIng:String = "";
		private var yourIng:String = "";
		private var finalResult:String = "";
		private var pass:Boolean = true;
		private var badThing:Boolean = false;
		
		private var needed:Array;
		private var player:Array;
		
		//NeededIngredients - ary1
		//YourIngredients - ary2
		public function Results(ary1:Array, ary2:Array, ordered:Boolean, scoreArray:Array, soupName:String) {
			Score_Array = scoreArray;
			SoupName = soupName;
			needed = ary1;
			player = ary2;
			
			for each (var str1:String in ary1){
				recipeIng += (str1);
				if (ary1.indexOf(str1) != ary1.length-1){
					recipeIng += ", ";
				}
			}
			
			for each (var str2:String in ary2){
				yourIng += (str2);
				if (ary2.indexOf(str2) != ary2.length-1){
					yourIng += ", ";
				}
			}
			
			tallyScore(ary1, ary2, ordered);
			
			addEventListener(Event.ADDED_TO_STAGE, onAdded);
		}
		
		private function onAdded(e:Event):void{
			removeEventListener(Event.ADDED_TO_STAGE, onAdded);
			
			recipeIngredients.text = recipeIng;
			yourIngredients.text = yourIng;
			rank.text = finalResult;
			
			if (badThing){
				if (player.indexOf("Cat") != -1 || player.indexOf("Sliced Cat"))
					soup.gotoAndStop("Fail2");
				else
					soup.gotoAndStop("Fail1");
			}else
				soup.gotoAndStop(SoupName);
			
			var sound:Sound;
			if (pass){
				cat.gotoAndStop(1);
				sound = new ClearSound();
			}else{
				cat.gotoAndStop(2);
				sound = new FailSound();
			}
			sound.play();
			
			//done.addEventListener(MouseEvent.CLICK, onDoneClick);
			addEventListener(MouseEvent.CLICK, onDoneClick);
		}
		
		private function tallyScore(ary1:Array, ary2:Array, ordered:Boolean):void{
			for each (var bad:String in Constants.BAD_INGREDIENTS){
				if (ary2.indexOf(bad) != -1){
					badThing = true;
					Score = 0;
					getGrade();
					return;
				}
			}
			
			if (ordered){
				//Sequence for Order-Specific Recipes
				var goal:int = ary1.length;
				var n:int = 0;
				if (ary1.length == ary2.length){
					for (var i:int = 0; i < ary1.length; i++){
						//Check if we've hit a 'Mix'
						if ((ary1[i] as String).substring(0, 2) == "Mix"){
							var mix1:Array = ((ary1[i] as String).split(':')[1] as String).split('+');
							
							//Check if player also has a mix
							if ((ary2[i] as String).substring(0, 2) == "Mix"){
								var mix2:Array = ((ary2[i] as String).split(':')[1] as String).split('+');
								var fail:Boolean = false;
								
								if (mix1.length == mix2.length){
									for each(var s:String in mix1){
										if (mix2.indexOf(s) == -1){
											fail = true;
											break;
										}
									}
									
									if (!fail){
										Score++;
										n++;
									}
								};
								
							}
						}else{
							if (ary1[i] == ary2[i])
								n++;
						}
					}
				}
				
				if (n == goal){
					Score+=2;
				}
			}
			
			for each (var str:String in ary1){
				trace(str);
				
				var num = ary2.indexOf(str);
				if ( num != -1){
					ary2.splice(num, 1);
					Score++;
				}
			}
			
			Score -= ( (.5 + (.5 * Settings.Difficulty)) * ary2.length );
			
			
			getGrade();
			
		}
		
		protected function getGrade():void{
			if ( Score >= Score_Array[0] ){
				trace( "You got an S!!!" );
				finalResult = "S";
			}else if ( Score < Score_Array[4] ){
				trace( "You got an F!" );
				finalResult = "F";
				pass = false;
			}else if ( Score < Score_Array[3] ){
				trace( "You got a D!" );
				finalResult = "D";
				pass = false;
			}else if ( Score < Score_Array[2] ){
				trace( "You got a C!" );
				finalResult = "C";
			}else if ( Score < Score_Array[1] ){
				trace( "You got a B!" );
				finalResult = "B";
			}else{
				trace( "You got an A!" );
				finalResult = "A";
			}
		}
		
		private function onDoneClick(e:MouseEvent):void{
			//done.removeEventListener(MouseEvent.CLICK, onDoneClick);
			removeEventListener(MouseEvent.CLICK, onDoneClick);
			
			var resultText:MovieClip;
			
			if (pass)
				resultText = new ClearText();
			else
				resultText = new FailText();
			
			resultText.x = 400; resultText.y = 300;
			addChild(resultText);
			TweenLite.from(resultText, 1, {alpha:0, scaleX:2, scaleY:2});
			
			//fadeToNextLevel();
			addEventListener(MouseEvent.CLICK, fadeToNextLevel);
		}
		
		private function fadeToNextLevel(e:MouseEvent):void{
			removeEventListener(MouseEvent.CLICK, fadeToNextLevel);
			
			var fade:MovieClip = new Fade();
			addChild(fade);
			TweenLite.from(fade, 1, {alpha:0, ease:Linear.easeNone, onComplete:loadNextLevel});
		}
		
		private function loadNextLevel():void{
			(this.root as Main).loadNextLevel(pass);
			Util.safeRemove(this);
		}

	}
	
}
